"""
__project__ = 'thc'
__file_name__ = 'Ddt_1'
__author__ = 'thc'
__time__ = '2021/1/3 14:05'
__product_name = PyCharm
-*- coding:utf-8 -*-
"""

import ddt
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
import unittest, time
import logging
import traceback

logging_1 = '%(asctime)s_%(name)s_%(levelname)s_%(message)s'
logging_2 = '%Y-%m-%d %H:%M:%S'
logging.basicConfig(
    level=logging.INFO,
    format=logging_1,
    datefmt=logging_2,
    filename='test_ddt.log',
    filemode='w'
)


@ddt.ddt
class TestDdt(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get('https://www.baidu.com')
        self.driver.implicitly_wait(10)

    @ddt.data(['郎平', '排球'],
              ['姚明', '篮球'])
    @ddt.unpack
    def test_1(self, testdata, expectdata):
        driver = self.driver
        try:
            driver.find_element_by_id('kw').send_keys(testdata)
            driver.find_element_by_id('su').click()
            time.sleep(3)
            self.assertTrue(expectdata in driver.page_source)
        except NoSuchElementException as e:
            logging.error('未定位到元素，报错信息为：'+str(traceback.format_exc()))
        except AssertionError as e:
            logging.error('搜索-"%s",预期-"%s",失败' %(testdata, expectdata))
        except Exception as e:
            logging.error('未知错误，错误信息为：'+str(traceback.format_exc()))
        else:
            logging.info('搜索-%s,预期-%s,成功' %(testdata, expectdata))

    def tearDown(self):
        self.driver.quit()


if __name__ == '__main__':
    unittest.main()